//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Text;
using LitwareHR.Recruiting.Syndication.Contract;
using System.Globalization;
using Shp.Runtime.Contract;
using LitwareHR.Recruiting.Syndication.Services.Properties;
using System.Net;

namespace LitwareHR.Recruiting.Syndication.Services
{
    public class RecruitingSydincationService : IRecruitingSyndicationService, IRecruitingPoxService
    {
        public SyndicationFeedFormatter GetOpenPositions(string tenantAlias)
        {         
            // Create a new Syndication Feed.
            string feedTitle = string.Format(CultureInfo.CurrentCulture, "Open positions for {0}", tenantAlias);
            SyndicationFeed feed = new SyndicationFeed(feedTitle, "A WCF Syndication Feed", null);
            List<SyndicationItem> items = new List<SyndicationItem>();

            // Create a new Syndication Item.
            StringPair[][] positions = SyndicationLogic.GetOpenPositionsByTenats(tenantAlias);
            foreach (StringPair[] position in positions)
            {
                string jobTitle = position.Where(p => p.Key == "title").SingleOrDefault().Value;
                string refCode = position.Where(p => p.Key == "code").SingleOrDefault().Value;
                string itemTitle = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", jobTitle, refCode);

                StringBuilder builder = new StringBuilder();
                position.Where(p => p.Key != "title" && p.Key != "code")
                        .ToList()
                        .ForEach(i => builder.AppendFormat("<strong>{0}</strong>: {1} </br>", 
                                                           i.Key, 
                                                           i.Value));

                string itemDescription = string.Format(CultureInfo.InvariantCulture, Resources.JobDescription, builder.ToString());

                SyndicationItem item = new SyndicationItem(itemTitle, itemDescription, null);
                items.Add(item);
            }
            
            feed.Items = items;
            Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);
            return formatter;
        }


        List<Position> IRecruitingPoxService.GetOpenPositions(string tenantName)
        {
            HttpResponseHeader cacheHeaderNoCache = HttpResponseHeader.CacheControl;
            HttpResponseHeader cacheHeaderPragma = HttpResponseHeader.Pragma;
            HttpResponseHeader cacheHeaderExpires = HttpResponseHeader.Expires;
            WebOperationContext.Current.OutgoingResponse.Headers.Add(cacheHeaderNoCache, "no-cache, must-revalidate, proxy-revalidate");
            WebOperationContext.Current.OutgoingResponse.Headers.Add(cacheHeaderPragma, "no-cache");
            WebOperationContext.Current.OutgoingResponse.Headers.Add(cacheHeaderExpires, "Mon, 26 Jul 1997 05:00:00 GMT");
            WebOperationContext.Current.OutgoingResponse.LastModified = DateTime.Now;

            List<Position> openPositions = new List<Position>();
            
            StringPair[][] positions = SyndicationLogic.GetOpenPositionsByTenats(tenantName);
            foreach (StringPair[] position in positions)
            {
                string jobTitle = position.Where(p => p.Key == "title").SingleOrDefault().Value;
                string refCode = position.Where(p => p.Key == "code").SingleOrDefault().Value;
                string itemTitle = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", jobTitle, refCode);

                StringBuilder builder = new StringBuilder();
                position.Where(p => p.Key != "title" && p.Key != "code")
                        .ToList()
                        .ForEach(i => builder.AppendFormat("<strong>{0}</strong>: {1} </br>",
                                                           i.Key,
                                                           i.Value));

                string itemDescription = string.Format(CultureInfo.InvariantCulture, Resources.JobDescription, builder.ToString());
                
                Position currentPosition = new Position() { JobTitle = jobTitle, RefCode = refCode, Description = itemDescription };
                openPositions.Add(currentPosition);
            }

            return openPositions;
        }

        
    }
}
